package com.xncoding.tomcat.socket02;


import java.io.IOException;
import java.io.InputStream;

public class Request {
    /**
     * 请求方式, eg: GET、POST
     */
    private String method;

    /**
     * 请求路径,eg: /index.html
     */
    private String url;

    /**
     * 请求信息输入流 <br>
     * 示例
     * <pre>
     *  GET / HTTP/1.1
     *  Host: localhost
     *  Connection: keep-alive
     *  Pragma: no-cache
     *  Cache-Control: no-cache
     *  Upgrade-Insecure-Requests: 1
     *  User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36
     * </pre>
     */
    private InputStream inputStream;

    public Request() {
    }

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }
        byte[] bytes = new byte[count];
        inputStream.read(bytes);
        // requestString 参考：this.inputStream 示例
        String requestString = new String(bytes);
        // 按换行分隔
        String[] requestStringArray = requestString.split("");
                // 读取第一行数据,即：GET / HTTP/1.1
                String firstLine = requestStringArray[0];
        // 遍历第一行数据按空格分隔
        String[] firstLineArray = firstLine.split(" ");
        this.method = firstLineArray[0];
        this.url = firstLineArray[1];
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
}